/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.caracol.db.AccesoDatos;
import com.caracol.db.CaracolQueryGenerator;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Roquet
 */
public class CursoRegistro {
    
    private static final String TABLE = "curso_registro";
    private static final String ID = "id";

    private int id;
    
    private static CaracolQueryGenerator queryGenerator = new CaracolQueryGenerator();
    
    private CursoRegistro(int id)
    {
        this.id = id;
    }
    
    public CursoRegistro() {
        this(0);
    }
    
    public static CursoRegistro getCursoRegistro(int id) throws SQLException, Exception
    {
        CursoRegistro cursoRegistro = null;
        
        String[] colums = new String[] { ID };
        Map<String, String> where = new LinkedHashMap<String, String>();
        where.put(ID, new Integer(id).toString());
        
        List<Object> rawArresto = queryGenerator.query(colums, TABLE, where, null, null, null, null);
        if (rawArresto != null && rawArresto.size() > 0) {
            List<Object> row = (List<Object>) rawArresto.get(0);
            cursoRegistro = new CursoRegistro();
            cursoRegistro.id = id;
        }
        
        return cursoRegistro;
    }
    
     /**
     * Get's all the arresto entitys in found in the database table
     * @return <code>List</code> containing all the rows in the table of the database
     * @throws SQLException if some database exception happend during the execution of this operation
     * @throws Exception if some exception happend during the execution of this operation
     */
    public static List<CursoRegistro> getAll() throws SQLException, Exception{
        ArrayList<CursoRegistro> cursoRegistros = new ArrayList<CursoRegistro>();
        String[] columns = new String[] { ID };
        List<Object> rawRegistroCargo = queryGenerator.query(columns, TABLE, null, null, null, null, null);
        
        for (Object object : rawRegistroCargo) {
            List<Object> row = (List<Object>) object;
            if (row != null && row.size() > 0) {
                List<Object> rawObject = (List<Object>) row.get(0);
                CursoRegistro arresto = new CursoRegistro();
                arresto.id = Integer.parseInt(rawObject.get(0).toString());
            }
        }
        
        return cursoRegistros;
    }
    
     /**
     * Saves the values of this instance in the database
     * @return true if the operations completes successfully otherwise returns false
     * @throws SQLException if some database exception happends during the excecution of this operation
     * @throws Exception if some exception happends during the excecution of this operation
     */
    public boolean save() throws SQLException, Exception {
        if (getId() > 0) { // this is already save no need to continue
            return true;
        }
        Map<String,Object> values = new LinkedHashMap<String,Object>();
        
        int rowsAffected = queryGenerator.insert(TABLE, values);
        
        ArrayList<Object> data = null;
        if (rowsAffected > 0) {
            AccesoDatos ad = queryGenerator.getOpenConnection();
            data = new ArrayList<Object>(ad.rawQuery("SELECT currval(\'sqarresto\');")); // get the id
        }
        
        if (data != null && data.size() > 0) {
            ArrayList<Object> row = (ArrayList<Object>) data.get(0);
            Double temp = Double.parseDouble(row.get(0).toString());
            this.id = temp.intValue();
            return true;
        }
        return false;
    }
    
    /** 
     * Delete the cargo from database this delete is made by searching in the database if this instance id exist
     * 
     * @return true if the operation affect more thant one row otherwise returns false
     * @throws SQLException if the database throws an exception during the execution of this operation
     * @throws Exception if something other than the database throws an exception this is throw
     * 
     */
    public boolean delete() throws SQLException, Exception {
        if (id <= 0) {
            return true;
        }

        Map<String, Object> conditions = new LinkedHashMap<String, Object>();
        conditions.put(ID, getId());
        int rowsAffected = queryGenerator.delete(TABLE, conditions);

        if (rowsAffected > 0) {
            id = 0;
        }

        return rowsAffected > 0;
    }
    

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }
    
}
